package org.jumiz.javafx.client.pages.commonents;

import com.alibaba.fastjson2.JSON;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class FormPane extends GridPane {

    private static Logger logger = LogManager.getLogger(FormPane.class);

    private AtomicInteger atomicInteger = new AtomicInteger(0);

    private ExtendMap<String, Control, CrudTablePane.TableConfig> controlMap = new ExtendMap<>();

    private Text errMesgTest = null;

    public FormPane() {
        super();
        setPadding(new Insets(10));
        setHgap(10);
        setVgap(10);
        getStyleClass().addAll("bg-white");
        setAlignment(Pos.TOP_CENTER);
        setPrefWidth(400);
        setPrefHeight(atomicInteger.get() * 40);
//        add(errMesgTest, 1, atomicInteger.addAndGet(1));
    }

    public void addControl(CrudTablePane.TableConfig fieldConfig, ControlType controlType) {
        Label label = new Label(fieldConfig.getLabel() + ": ");
        label.setPrefWidth(120);
        label.setAlignment(Pos.CENTER_RIGHT);
        label.setPadding(new Insets(5));
        label.getStyleClass().addAll("bold-label");

        Control control = null;
        switch (controlType) {
            case TextField:
                TextField textField = new TextField();
                textField.setPrefWidth(360);
                textField.setPromptText("请输入" + fieldConfig.getLabel());
                textField.setPadding(new Insets(5));
                textField.setAlignment(Pos.CENTER_LEFT);
                control = textField;
                break;
        }

        add(label, 0, atomicInteger.addAndGet(1));
        add(control, 1, atomicInteger.get());
        controlMap.putObject(fieldConfig.getField(), control, fieldConfig);

        setPrefHeight((atomicInteger.get() + 1) * 44);
    }

    public Map<String, Object> getFormData() {
        Map<String, Object> collect = controlMap.valueToMap().entrySet().stream().collect(Collectors.toMap(item -> item.getKey(), item -> ((TextField) item.getValue()).getText()));
        return collect;
    }

    public CrudTablePane.Valid valid(List<CrudTablePane.Rule> rules) {
        for (int i = 0; i < rules.size(); i++) {
            CrudTablePane.Rule rule = rules.get(i);
            CrudTablePane.Valid valid = valid(rule);
            if (!valid.isValid()) {
                return valid;
            }
        }
        CrudTablePane.Valid valid = new CrudTablePane.Valid();
        valid.setValid(true);
        return valid;
    }

    public CrudTablePane.Valid valid(CrudTablePane.Rule rule) {
        logger.info("校验: " + JSON.toJSONString(rule));
        initErrMesgTest();

        CrudTablePane.Valid valid = new CrudTablePane.Valid();
        Control control = controlMap.getObjectValue(rule.getField());
        if (control == null) {
            valid.setValid(true);
            return valid;
        }
        if (rule.getRequired() != null && rule.getRequired().booleanValue()) {
            String text = ((TextField) control).getText();
            if (text == null || text.trim().length() == 0) {
                valid.setValid(false);
                valid.setErrMesg("[" + controlMap.getObjectExtend(rule.getField()).getLabel() + "]" +  "不能为空");

                control.getStyleClass().add("form-error");

                errMesgTest.setVisible(true);
                errMesgTest.setText(valid.getErrMesg());

                return valid;
            } else {
                control.getStyleClass().remove("form-error");
                errMesgTest.setText("");
                errMesgTest.setVisible(false);
            }
        }
        valid.setValid(true);
        return valid;
    }

    private void initErrMesgTest() {
        if (errMesgTest == null) {
            errMesgTest = new Text("");
            errMesgTest.setVisible(false);
            errMesgTest.setFill(Color.RED);
            add(errMesgTest, 1, atomicInteger.addAndGet(1));
        }
    }

    public enum ControlType {
        TextField
    }

}
